cd "$replication\code\"

use "${replication}\data\for_event_elecXroad.dta", clear

global hist_options "freq  ytitle("Villages" " ")   graphregion(color(white)) ylabel(,format(%12.0g))"

label var bhuvan_dry "Land cropped during rabi season (ISRO)"
label var winter_cropping "Land cropped during rabi season (Jain et al.)"
label var ndvi_delta_r_ "Change in NDVI during rabi season (Asher & Novosad)"
label var icw_irrigation "Dry season cropping index"

global depvars "bhuvan_dry winter_cropping ndvi_delta_r_ "
matrix results = J(3,3,.)
matrix rownames results = $depvars




local i=0
foreach var in  $depvars icw_irrigation {
	
	
	if "`var'"!="icw_irrigation" {
		local i=`i'+1
		qui twoway (hist `var' if year==2005 & `var'>0, $hist_options color(green) ) (hist `var' if year==2014 & `var'>0, $hist_options fcolor(none) lcolor(black) ), legend(order(1 "2005" 2 "2014" ))
		qui graph export "..\Output\hist_`var'.png", replace
		sum `var' if  year==2005, mean
		local N_total=`r(N)'	
		disp "Number of villages with non-missing value of `var' = `N_total'"
		matrix results[`i',1] = `N_total'
		local j=1
		foreach year in 2005 2014 {
			local j=`j'+1
			sum `var' if `var'<=0 & year==`year', mean
			local N_nonpositive=`r(N)'
			disp "Number of villages with non-positive value of `var' in `year' = `N_nonpositive'"
			matrix results[`i',`j'] = `N_nonpositive'
		}
	}
	else {
		qui twoway (hist `var' if year==2005 , $hist_options color(green) ) (hist `var' if year==2014 , $hist_options fcolor(none) lcolor(black) ), legend(order(1 "2005" 2 "2014" ))
		qui graph export "..\Output\hist_`var'.png", replace
	}
	
}

matrix list results

local titles "& \shortstack{Number of  villages \\ with observations}  & \multicolumn{2}{c}{\shortstack{Number of villages with \\ zero or negative values}} \\ \cmidrule(lr){3-4}  \addlinespace[-2ex]  \\ & & in 2005 & in 2014 \\"
local numbers "& (1) & (2) & (3)  \\ \hline"
esttab matrix(results, fmt(%12.2g)) using "..\Output\nonzeros.tex", label booktabs nonote collabels(,none) replace mlabels(,none) nomtitles  nonumbers posthead("`titles'" "`numbers'")



